using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;
using System.Threading;

namespace UusAhi
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game : Microsoft.Xna.Framework.Game
    {
        public static ContentManager content;
        public static Random rand;
        public List<Player> players;
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
            
        protected SpriteFont fpsFont;
        protected float ups = 1f;


        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            rand = new Random();
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
            
            // Change of resolution.
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            IsFixedTimeStep = false; // CPU hog :(

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            base.Initialize();

            players = new List<Player>();
            players.Add(new Player("Player1"));     
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Game.content = Content;
            fpsFont = Content.Load<SpriteFont>("Fonts/FpsFont");

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            if ((float)gameTime.ElapsedGameTime.TotalSeconds != 0)
            {
                ups = 1 / (float)gameTime.ElapsedGameTime.TotalSeconds; // updates per second calculation
            }

        
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            float frameRate = 1f;
            if ((float)gameTime.ElapsedGameTime.TotalSeconds != 0 )
            {
                frameRate = 1 / (float)gameTime.ElapsedGameTime.TotalSeconds; // FPS calculation
            }

            
            spriteBatch.Begin();

            spriteBatch.DrawString(fpsFont,"FPS: " + Math.Round(frameRate,1).ToString(),new Vector2(5,5),Color.Black);
            spriteBatch.DrawString(fpsFont, "UPS: " + Math.Round(ups, 1).ToString(), new Vector2(5, 15), Color.Black);

            foreach (Player player in players)
            {
                player.Draw(spriteBatch);
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
